package cn.iocoder.yudao.module.erp.controller.admin.payrecord;

import cn.hutool.core.collection.CollUtil;
import cn.iocoder.yudao.framework.common.util.collection.MapUtils;
import cn.iocoder.yudao.framework.dict.core.DictFrameworkUtils;
import cn.iocoder.yudao.module.erp.controller.admin.customeraccount.vo.CustomerAccountRespVO;
import cn.iocoder.yudao.module.erp.dal.dataobject.customeraccount.CustomerAccountDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.customerinfo.CustomerInfoDO;
import cn.iocoder.yudao.module.erp.dal.dataobject.payinfo.PayInfoDO;
import cn.iocoder.yudao.module.erp.enums.DictTypeConstants;
import cn.iocoder.yudao.module.erp.service.customerinfo.CustomerInfoService;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;
import java.util.stream.Collectors;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;
import static cn.iocoder.yudao.framework.common.util.collection.CollectionUtils.convertSet;

import cn.iocoder.yudao.module.erp.controller.admin.payrecord.vo.*;
import cn.iocoder.yudao.module.erp.dal.dataobject.payrecord.PayRecordDO;
import cn.iocoder.yudao.module.erp.service.payrecord.PayRecordService;

@Tag(name = "管理后台 - 付款记录")
@RestController
@RequestMapping("/erp/pay-record")
@Validated
public class PayRecordController {

    @Resource
    private PayRecordService payRecordService;

    @Resource
    private CustomerInfoService customerInfoService;

    @PostMapping("/create")
    @Operation(summary = "创建付款记录")
    @PreAuthorize("@ss.hasPermission('erp:pay-record:create')")
    public CommonResult<Long> createPayRecord(@Valid @RequestBody PayRecordSaveReqVO createReqVO) {
        return success(payRecordService.createPayRecord(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新付款记录")
    @PreAuthorize("@ss.hasPermission('erp:pay-record:update')")
    public CommonResult<Boolean> updatePayRecord(@Valid @RequestBody PayRecordSaveReqVO updateReqVO) {
        payRecordService.updatePayRecord(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除付款记录")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('erp:pay-record:delete')")
    public CommonResult<Boolean> deletePayRecord(@RequestParam("id") Long id) {
        payRecordService.deletePayRecord(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得付款记录")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('erp:pay-record:query')")
    public CommonResult<PayRecordRespVO> getPayRecord(@RequestParam("id") Long id) {
        PayRecordDO payRecord = payRecordService.getPayRecord(id);



        return success(BeanUtils.toBean(payRecord, PayRecordRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得付款记录分页")
    @PreAuthorize("@ss.hasPermission('erp:pay-record:query')")
    public CommonResult<PageResult<PayRecordRespVO>> getPayRecordPage(@Valid PayRecordPageReqVO pageReqVO) {
        PageResult<PayRecordDO> pageResult = payRecordService.getPayRecordPage(pageReqVO);


        if (CollUtil.isEmpty(pageResult.getList())) {
            return success(PageResult.empty(pageResult.getTotal()));
        }

        // 客户信息
        List<CustomerInfoDO> customerList = customerInfoService.getCustomerListIds(
                convertSet(pageResult.getList(), PayRecordDO::getCustomerId));
        Map<Long,CustomerInfoDO> customerMap=customerList.stream().collect(Collectors.toMap(CustomerInfoDO::getId, e -> e));


        return success(BeanUtils.toBean(pageResult, PayRecordRespVO.class, record -> {

            MapUtils.findAndThen(customerMap, record.getCustomerId(), cus -> record.setCustomerName(cus.getName()));

            record.setTypeName(DictFrameworkUtils.getDictDataLabel(DictTypeConstants.LNG_GET_OR_PAY, record.getType()));
           // record.setMoneyCount(record.getMoneyCount());
        }));


        

       // return success(BeanUtils.toBean(pageResult, PayRecordRespVO.class));
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出付款记录 Excel")
    @PreAuthorize("@ss.hasPermission('erp:pay-record:export')")
    @ApiAccessLog(operateType = EXPORT)
    public void exportPayRecordExcel(@Valid PayRecordPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<PayRecordDO> list = payRecordService.getPayRecordPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "付款记录.xls", "数据", PayRecordRespVO.class,
                        BeanUtils.toBean(list, PayRecordRespVO.class));
    }

}